package com.example.demo.controller.order;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.demo.controller.BaseController;
import com.example.demo.domain.AjaxResult;
import com.example.demo.domain.order.SaleOrder;
import com.example.demo.domain.userInfo.UserInfo;
import com.example.demo.service.order.SaleOrderService;
import com.example.demo.web.TableDataInfo;

@Controller
@RequestMapping("/order")
public class SaleOrderController extends BaseController {
    @Autowired
    private SaleOrderService saleOrderService;

    //加载管理的页面
    @GetMapping("")
    public String service() {
        return "order/SaleOrder";
    }

    //查询方法，并将结果渲染到表格中
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SaleOrder saleOrder) {
        startPage();
        if (saleOrder.getDelFlag() == null) {
            saleOrder.setDelFlag(1);
        } else {
            saleOrder.setDelFlag(2);
        }
        List<SaleOrder> list = saleOrderService.selectSaleOrderList(saleOrder);
        return getDataTable(list);
    }


    @GetMapping("/add")
    public String add() {
        return "service" + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(SaleOrder saleOrder) {
        return toAjax(saleOrderService.insert(saleOrder));
    }

    /**
     * 修改
     */
    //从前端获取选中的信息
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Integer orderId, ModelMap mmap) {
        SaleOrder saleOrder = saleOrderService.selectByPrimaryKey(orderId);
        mmap.put("saleOrder", saleOrder);
        return "order/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SaleOrder saleOrder) {

        return toAjax(saleOrderService.updateByPrimaryKeySelective(saleOrder));
    }

    @GetMapping("/chargeBack/{orderId}")
    public String chargeBack(@PathVariable("orderId") Integer orderId, ModelMap mmap) {
        SaleOrder saleOrder = saleOrderService.selectByPrimaryKey(orderId);
        mmap.put("saleOrder", saleOrder);
        return "cashier/ChargeBack";
    }

    @PostMapping("/chargeBack")
    @ResponseBody
    public AjaxResult chargeBack(SaleOrder saleOrder) {
        saleOrder.setDelFlag(2);
        return toAjax(saleOrderService.updateByPrimaryKeySelective(saleOrder));
    }


//	@PostMapping("/remove")
//	@ResponseBody
//	public AjaxResult remove(String ids) {			
//		String[] id = ids.split(",");
//		return toAjax(saleOrderService.deleteServiceByIds(id));
//	}

    //查询服务类型名称
    @PostMapping("/selectBname")
    @ResponseBody
    public List<UserInfo> selectBarberName() {
        // TODO Auto-generated method stub
        return this.saleOrderService.selectBarberName();
    }

    @PostMapping("/selectCname")
    @ResponseBody
    public List<UserInfo> selectCashierName() {
        // TODO Auto-generated method stub
        return this.saleOrderService.selectCashierName();
    }

    @GetMapping("/statistics")
    public String statistics() {
        return "statistics/Statistics";
    }



    @RequestMapping("/sumTotalByMonth")
    @ResponseBody
    public List<SaleOrder> sumByMonth(String year) {
        //默认查询当前的年份
        if (year == null || year == "") {
            Calendar date = Calendar.getInstance();
            year = String.valueOf(date.get(Calendar.YEAR));

        }

        List<SaleOrder> saleOrderList = saleOrderService.sumTotalByMonth(year);
        for (SaleOrder saleOrder : saleOrderList)
            saleOrder.setYear(year);
        return saleOrderList;
    }

    @RequestMapping("/compareTotalByYears")
    @ResponseBody
    public List<SaleOrder> compareTotalByYears() {
        List<SaleOrder> saleOrderList = saleOrderService.compareTotalByYears();
        return saleOrderList;
    }

    @RequestMapping("/compareByQuarter")
    @ResponseBody
    public List<SaleOrder> compareByQuarter(String year) {
        //默认查询当前的年份
        if (year == null || year == "") {
            Calendar date = Calendar.getInstance();
            year = String.valueOf(date.get(Calendar.YEAR));

        }
        List<SaleOrder> saleOrderList = saleOrderService.compareByQuarter(year);
        for (SaleOrder saleOrder : saleOrderList)
            saleOrder.setYear(year);
        return saleOrderList;

    }

    @RequestMapping("/inSevenDays")
    @ResponseBody
    public List<SaleOrder> inSevenDays() {

        List<SaleOrder> saleOrderList = saleOrderService.inSevenDays();

        return saleOrderList;

    }

    @GetMapping("/sumByCategory")
    public String sumByCatelogory() {

        return "statistics/SumTotalByChoice";
    }

    @RequestMapping("/sumByBarber")
    @ResponseBody
    public List<SaleOrder> sumByBarBer(SaleOrder sale) {
        List<SaleOrder> saleOrderList = saleOrderService.sumTotalByBarber(sale);
        if(sale!=null){
        for (SaleOrder saleOrder : saleOrderList){
            saleOrder.setScreateTime(sale.getScreateTime());
            saleOrder.setFcreateTime(sale.getFcreateTime());
        }
        }
        return saleOrderList;
    }
     //按价格区间统计
    @RequestMapping("/sumByService")
    @ResponseBody
    public List<SaleOrder> sumByService(SaleOrder saleOrder){

        List<SaleOrder> saleOrderList = saleOrderService.sumByService(saleOrder);

        if(saleOrder!=null){
        for (SaleOrder sale : saleOrderList){
            sale.setScreateTime(saleOrder.getScreateTime());
            sale.setFcreateTime(saleOrder.getFcreateTime());
        }
        }
        return  saleOrderList;
    }

    //按服务类型统计
    @RequestMapping("/sumByTos")
    @ResponseBody
    public List<SaleOrder> sumByTos(SaleOrder saleOrder) {

        List<SaleOrder> saleOrderList = saleOrderService.sumByTos(saleOrder);
        if(saleOrder!=null){
            for (SaleOrder sale : saleOrderList){
                sale.setScreateTime(saleOrder.getScreateTime());
                sale.setFcreateTime(saleOrder.getFcreateTime());
            }
        }
        return  saleOrderList;
    }


}
